import axios from 'axios'
import { ElLoading } from 'element-plus'

class fetchInstance {

    instance
    interceptors
    isShowLoading
    loadingCount = 0
    addLoading() {
        this.isShowLoading = ElLoading.service({
            lock: true,
            text: 'Loading',
            background: 'rgba(0, 0, 0, 0.7)',
        })
        this.loadingCount++
    }

    isCloseLoading() {

        this.loadingCount--
        if (this.loadingCount < 1) {
            this.isShowLoading.close()
            this.loadingCount = 0
        }
    }
    resetLoading() {
        this.isShowLoading.close()
        this.loadingCount = 0
    }

    constructor(config) {
        this.instance = axios.create(config)
        this.interceptors = config.interceptors

        this.instance.interceptors.request.use(
            this.interceptors?.requestInterceptor,
            this.interceptors?.requestInterceptorCatch
        )

        this.instance.interceptors.response.use(
            this.interceptors?.responseInterceptor,
            this.interceptors?.responseInterceptorCatch
        )


        this.instance.interceptors.request.use(
            (config) => {
                this.addLoading()
                return config
            },
            (err) => {
                this.resetLoading()
                return Promise.reject(err);
            }
        )
        this.instance.interceptors.response.use(
            (res) => {
                this.isCloseLoading()
                if (res.data?.code === 500 || res.data?.code === 401) {
                    this.resetLoading()
                }
                return res
            },
            (err) => {
                this.resetLoading()
                return Promise.reject(err);
            }
        )
    }



    request(config) {
        return new Promise((resolve, reject) => {
            if (config.interceptors?.requestInterceptor) {
                config = config.interceptors.requestInterceptor(config)
            }

            this.instance
                .request(config)
                .then((res) => {
                    if (config.interceptors?.responseInterceptor) {
                        res = config.interceptors.responseInterceptor(res)
                    }
                    if (res instanceof Error) {
                        reject(res)
                        return
                    }
                    resolve(res)
                })
                .catch((err) => {
                    this.resetLoading()
                    reject(err)
                    return Promise.reject(err);
                })
        })

    }

    // get请求
    get(config) {
        return this.request({ ...config, method: 'GET' })
    }
    // post请求
    post(config) {
        return this.request({ ...config, method: 'POST' })
    }
    // delete请求
    delete(config) {
        return this.request({ ...config, method: 'DELETE' })
    }
    // patch请求
    patch(config) {
        return this.request({ ...config, method: 'PATCH' })
    }
    // put请求
    put(config) {
        return this.request({ ...config, method: 'PUT' })
    }
}

export default fetchInstance
